package cn.iocoder.yudao.module.handguard.service.userbasetable7;

import cn.iocoder.yudao.module.handguard.dal.dataobject.carrecord.CarRecordDO;
import cn.iocoder.yudao.module.handguard.dal.mysql.carrecord.CarRecordMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.handguard.controller.app.userbasetable7.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.userbasetable7.UserBaseTable7DO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.handguard.convert.userbasetable7.UserBaseTable7Convert;
import cn.iocoder.yudao.module.handguard.dal.mysql.userbasetable7.UserBaseTable7Mapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.handguard.enums.ErrorCodeConstants.*;

/**
 * 全款车按揭车信贷 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserBaseTable7ServiceImpl implements UserBaseTable7Service {

    @Resource
    private UserBaseTable7Mapper userBaseTable7Mapper;

    @Resource
    private CarRecordMapper carRecordMapper;

    @Override
    @Transactional
    public Long createUserBaseTable7(UserBaseTable7CreateReqVO createReqVO) {
        // 插入
        UserBaseTable7DO userBaseTable7 = UserBaseTable7Convert.INSTANCE.convert(createReqVO);
        //copy 车辆信息表
        CarRecordDO carRecordDO = new CarRecordDO();
        BeanUtils.copyProperties(userBaseTable7,carRecordDO);
        carRecordMapper.insert(carRecordDO);

        userBaseTable7Mapper.insert(userBaseTable7);

        // 返回
        return userBaseTable7.getId();
    }

    @Override
    public void updateUserBaseTable7(UserBaseTable7UpdateReqVO updateReqVO) {
        // 校验存在
        validateUserBaseTable7Exists(updateReqVO.getId());
        // 更新
        UserBaseTable7DO updateObj = UserBaseTable7Convert.INSTANCE.convert(updateReqVO);
        userBaseTable7Mapper.updateById(updateObj);
    }

    @Override
    public void deleteUserBaseTable7(Long id) {
        // 校验存在
        validateUserBaseTable7Exists(id);
        // 删除
        userBaseTable7Mapper.deleteById(id);
    }

    private void validateUserBaseTable7Exists(Long id) {
        if (userBaseTable7Mapper.selectById(id) == null) {
            throw exception(USER_BASE_TABLE7_NOT_EXISTS);
        }
    }

    @Override
    public UserBaseTable7DO getUserBaseTable7(Long id) {
//        LambdaQueryWrapper<UserBaseTable7DO> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(UserBaseTable7DO::getUserId,id);
        return userBaseTable7Mapper.selectById(id);
    }

    @Override
    public List<UserBaseTable7DO> getUserBaseTable7List(Long id) {
        LambdaQueryWrapper<UserBaseTable7DO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserBaseTable7DO::getApplicationId,id);
        return userBaseTable7Mapper.selectList(queryWrapper);
    }

    @Override
    public PageResult<UserBaseTable7DO> getUserBaseTable7Page(UserBaseTable7PageReqVO pageReqVO) {
        return userBaseTable7Mapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserBaseTable7DO> getUserBaseTable7List(UserBaseTable7ExportReqVO exportReqVO) {
        return userBaseTable7Mapper.selectList(exportReqVO);
    }

}
